package prionityQueue;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yi
 * Date: 2024-07-25
 * Time: 10:39
 */
public class MyHeap {
    private int[] elem;
    private int usedSize;

    public MyHeap(){
        this.elem = new int[10];
    }

    public void init(int[] array){
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }
    }

    //实现大头堆
    public void createHeap(){
        for (int parent = (this.usedSize-1-1)/2; parent >= 0; parent--) {
            shiftDown(parent,usedSize);
        }
    }

    private void shiftDown(int parent, int usedSize) {
        int child = parent*2+1;
        while(child < usedSize) {
            if (child+1 < usedSize && this.elem[child + 1] > this.elem[child]) {
                child = child + 1;
            }
            if (this.elem[parent] < this.elem[child]) {
                int tmp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = tmp;
                parent = child;
                child = parent*2+1;
            }else{
                break;
            }
        }
    }

    //实现小头堆
    public void createHeap2(){
        for (int parent = (this.usedSize-1-1)/2; parent >= 0; parent--) {
            shiftUp(parent,usedSize);
        }
    }

    private void shiftUp(int parent, int usedSize) {
        int child = parent*2+1;
        while(child < usedSize){
            if(child+1 < usedSize || this.elem[child+1] < this.elem[child]){
                child++;
            }
            if(this.elem[child] < this.elem[parent]){
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = parent*2+1;
            }else{
                return;
            }
        }
    }

    //堆的插入
    public void offer(int val){
        if(this.usedSize == this.elem.length){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
        int child = this.usedSize;
        int parent = (child-1)/2;
        while(child != 0){
            if(this.elem[child] > this.elem[parent]){
                int tmp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = tmp;
                child = parent;
                parent = (child-1)/2;
            }else{
                return;
            }
        }
    }

    //堆的删除
    public int poll(){
        int ret = this.elem[0];
        int tmp = this.elem[0];
        this.elem[0] = this.elem[this.usedSize-1];
        this.elem[this.usedSize-1] = tmp;
        this.usedSize--;
        shiftDown(0,usedSize);
        return ret;
    }

    public int peek(){
        return this.elem[0];
    }

    //堆排序
    public void heapSort(){
        int end = this.usedSize - 1;
        while(end > 0){
            int tmp = this.elem[0];
            this.elem[0] = this.elem[end];
            this.elem[end] = tmp;
            this.shiftDown(0,end);
            end--;
        }
    }
}
